package com.baiyang.server.service.base.impl;


import com.baiyang.server.model.system.Resource;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.system.ResourceService;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.FileTool;
import com.baiyang.server.tools.Letter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.base.NoticeDao;
import com.baiyang.server.model.base.Notice;
import com.baiyang.server.service.base.NoticeService;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;

/**
 * @author XVX
 */
@Service	
public class NoticeServiceImpl extends BaseServiceImpl<NoticeDao, Notice> implements NoticeService{
    @Autowired
    private NoticeDao noticeDao = null;
    @Autowired
    private ResourceService resourceService = null;
    /**上传地址*/
    @Value("${file.upload.path}")
    private String filePath;
    /**
     * 首先将content写入本地文件
     * 将notice写入到数据库
     * 将资源信息吸入数据库
     * @param notice
     * @param letter
     */
    @Override
    @Transactional
    public void insertNotice(Notice notice, Letter<String> letter) {
        // 内容写到本地
        String contentName = DataTool.createUUID() + ".html";
        String ymd = DataTool.sdf_ymd.format(new Date());
        String path = filePath + File.separator + ymd;
        if(!FileTool.write(path,contentName,notice.getContent())){
            letter.setResult("FAIL");
            letter.setDetail("公告内容写入本地出错");
            return ;
        }
        // 公告基本信息写入数据库
        noticeDao.insert(notice);
        //资源记录数据库
        //1.html资源
        Resource resourceHtml = new Resource();
        resourceHtml.setId(DataTool.createUUID());
        resourceHtml.setCreateBy(notice.getCreateBy());
        resourceHtml.setRelationId(notice.getId());
        resourceHtml.setRelationType("CONTENT");
        resourceHtml.setType("HTML");
        resourceHtml.setPath(ymd + File.separator + contentName);
        resourceService.insert(resourceHtml);
        //2.图片资源
        Resource resourcePic = new Resource();
        resourcePic.setId(DataTool.createUUID());
        resourcePic.setCreateBy(notice.getCreateBy());
        resourcePic.setRelationId(notice.getId());
        resourcePic.setRelationType("TITLE_PIC");
        resourcePic.setType("PIC");
        resourcePic.setPath(notice.getTitlePic());
        resourceService.insert(resourcePic);
        // 插入成功
        letter.setDetail("数据插入成功！");
        letter.setResult("SUCCESS");
    }


    /**
     * 获取详细信息
     * @param noticeId
     * @return
     */
    @Override
    public String getNotice(String noticeId) {
        // 更改阅读数量
        noticeDao.readById(noticeId);

        Resource resource = new Resource();
        resource.setRelationId(noticeId);
        resource.setType("HTML");
        resource.setRelationType("CONTENT");
        resource = resourceService.selectOne(resource);
        if(resource != null){
            String path = resource.getPath();
            String html = FileTool.read(filePath + File.separator + path);
            return html;
        }
        return null;
    }
}
